package com.example.myfirstapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.nfc.Tag;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.TextureView;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;

public class EditTextActivity extends AppCompatActivity {
    private static final String TAG = "EditTextActivity";
    private  static final File file = new File("data/data/com.example.myfirstapplication/cache/info.txt");
    //按钮: 登录
    private Button mbtn7;
    //文本输入框:用户名
    private EditText met1;
    //文本输入框:,密码
    private EditText met2;
    //选择框
    private CheckBox cb1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_text);

        mbtn7 = findViewById(R.id.btn_7);
        met1  = findViewById(R.id.et_1);
        met2 = findViewById(R.id.et_2);
        cb1 = findViewById(R.id.cb_1);

        //自动写入密码
        if(file.exists()&&file.length()>0){
            BufferedReader br =null;
            FileInputStream fis =null;
            try{
            fis = new FileInputStream(file);
            br = new BufferedReader(new InputStreamReader(fis));
            String info = br.readLine();
            String user = info.split("##")[0];
            String pwd = info.split("##")[1];
            met1.setText(user);
            met2.setText(pwd);
           } catch (FileNotFoundException e) {
               e.printStackTrace();
           } catch (IOException e) {
               e.printStackTrace();
           }catch(Exception e){
               e.printStackTrace();
           }finally{
               try {
                   if (br != null) br.close();
                   if (fis != null) fis.close();
               } catch (IOException e) {
                   e.printStackTrace();
               }catch (Exception e) {
                   e.printStackTrace();
               }
           }

        }

        //点击事件监听器
        mbtn7.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                获取用户名和密码
                String user = met1.getText().toString().trim();
                String pwd = met2.getText().toString().trim();
                if(TextUtils.isEmpty(user)||TextUtils.isEmpty(pwd)) {// 用户名或密码为空,直接退出
                    Toast.makeText(EditTextActivity.this,"用户名名或密码不能为空",Toast.LENGTH_SHORT).show();
                    return;
                }else{//若不为空,则进行登录判断
                    if(user.equals("cxk")&&pwd.equals(("abcd"))){
                        Toast.makeText(EditTextActivity.this, "登录成功!", Toast.LENGTH_SHORT).show();
                        //登录成功就将文件保存在info.text文件夹之下
                        FileOutputStream fos=null;
                        try {
                            fos = new FileOutputStream(file);
                            //保存格式100000##abc
                            fos.write((user +"##"+pwd).getBytes());
                            Toast.makeText(EditTextActivity.this,"登录成功",Toast.LENGTH_SHORT).show();
                        } catch (FileNotFoundException e) {
                            e.printStackTrace();
                            Toast.makeText(EditTextActivity.this,"系统异常", Toast.LENGTH_SHORT).show();
                        } catch (IOException e) {
                            e.printStackTrace();
                            Toast.makeText(EditTextActivity.this,"系统异常", Toast.LENGTH_SHORT).show();
                        }catch(Exception e){
                            e.printStackTrace();
                            Toast.makeText(EditTextActivity.this,"系统异常", Toast.LENGTH_SHORT).show();
                        }finally {
                            if( fos !=null) {
                                try {
                                    fos.close();
                                } catch (IOException e) {
                                    e.printStackTrace();
                                }
                            }
                        }

                    }else{
                        Toast.makeText(EditTextActivity.this,"用户名或密码错误", Toast.LENGTH_SHORT).show();
                    }
                }
                if(cb1.isChecked()){            //确定是否需要保存密码   选择了为true 没选择为false
                    Log.i(TAG,"记住密码");
                }else{
                    Log.i(TAG,"不需要记住密码");
                }
            }
        });


        //文本输入监听器
        met1.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                Log.d("edittext",s.toString());
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }
}
